import react from "@vitejs/plugin-react";
import { fileURLToPath, URL } from "url";
import vitePluginImp from "vite-plugin-imp";

// https://vitejs.dev/config/
export default ({ mode }) => {
  return {
    base: "./",
    define: {
      "process.env.NODE_ENV": `'${mode}'`,
    },
    build: {
      assetsInlineLimit: 0,
      minify: "terser",
      terserOptions: {
        compress: {
          drop_console: false,
          drop_debugger: true,
        },
      },
      chunkSizeWarningLimit: 1024 * 100,
    },
    plugins: [
      react(),
      vitePluginImp({
        optimize: false,
        libList: [
          {
            libName: "antd",
            libDirectory: "es",
            style: (name) => `antd/es/${name}/style`,
          },
        ],
      }),
    ],
    resolve: {
      alias: {
        "@components": fileURLToPath(
          new URL("./src/components", import.meta.url)
        ),
        "@hashHistory": fileURLToPath(
          new URL("./src/hashHistory", import.meta.url)
        ),
        "@service": fileURLToPath(new URL("./src/service", import.meta.url)),
        "@utils": fileURLToPath(
          new URL("./src/utils/utils.ts", import.meta.url)
        ),
        "@views": fileURLToPath(new URL("./src/views", import.meta.url)),
        "@globalStore": fileURLToPath(
          new URL("./src/globalStore.ts", import.meta.url)
        ),
        "three-base": fileURLToPath(new URL("./three-base", import.meta.url)),
      },
    },
    css: {
      preprocessorOptions: {
        less: {
          javascriptEnabled: true, // 支持内联 javascript
        },
      },
    },
    server: {
      port: 5333,
      //   headers: {
      //     "Cross-Origin-Resource-Policy": "cross-origin",
      //     "Cross-Origin-Embedder-Policy": "require-corp",
      //     "Cross-Origin-Opener-Policy": "same-origin",
      //   },
    },
  };
};
